package com.vogue.supplymall.admin.business.common;

import com.vogue.gear.memory.provider.IMemProvider;
import com.vogue.supplymall.admin.business.common.constant.CachedPayValues;
import com.vogue.supplymall.admin.business.common.constant.CachedTsPayValues;
import com.vogue.supplymall.admin.business.domain.model.AdminPrivatePlatform;
import com.vogue.supplymall.admin.business.domain.model.AdminPrivatePlatformApp;
import com.vogue.supplymall.admin.business.domain.service.AdminPrivatePlatformAppService;
import com.vogue.supplymall.admin.business.domain.service.AdminPrivatePlatformService;
import com.vogue.supplymall.common.constant.ConstantBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Admin mem utils.
 *
 * @author zhangliang  Create in 2018-09-12
 */
@Component
public class AdminMemUtils implements ApplicationRunner {
    @Resource(name = "memProvider")
    private IMemProvider<Object> memProvider;
    @Resource
    private AdminPrivatePlatformService adminPrivatePlatformService;
    @Resource
    private AdminPrivatePlatformAppService adminPrivatePlatformAppService;
    @Resource
    private CachedPayValues cachedPayValues;
    @Resource
    private CachedTsPayValues cachedTsPayValues;

    /**
     * The constant LOGGER_ADMINMEMUTILS.
     */
    public static final Logger LOGGER_ADMINMEMUTILS = LoggerFactory.getLogger(AdminMemUtils.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        LOGGER_ADMINMEMUTILS.info("process AdminMemUtils method setPlatform()");
        setPlatform();
    }

    /**
     * Gets platform.
     * 获取缓存中私有平台的数据
     *
     * @return the platform
     */
    public Map<String, Map<String, String>> getPlatform() {
        Map<String, Map<String, String>> platform = (Map<String, Map<String, String>>) memProvider.get(ConstantBean.PLATFORM_PROVIDER_NAME);
        if (null == platform) {
            platform = setPlatform();
        }

        return platform;
    }

    /**
     * Sets platform.
     * 设置缓存重私有平台的数据
     *
     * @return the platform
     */
    public Map<String, Map<String, String>> setPlatform() {
        Map<String, Map<String, String>> platform = new HashMap<>();
        List<AdminPrivatePlatform> platformList = adminPrivatePlatformService.findAll();
        for (AdminPrivatePlatform privatePlatform : platformList) {
            List<AdminPrivatePlatformApp> apps = adminPrivatePlatformAppService.selectByWhr(" appid = " + privatePlatform.getId());
            Map<String, String> map = new HashMap<>();
            map.put("tname", privatePlatform.getTname());
            map.put("code", privatePlatform.getPlatform().toString());
            map.put("name", privatePlatform.getName());
            map.put("entrymode", privatePlatform.getEntrymode().toString());
            map.put("paytogether", privatePlatform.getPaytogether().toString());
            for (AdminPrivatePlatformApp app : apps) {
                if (app.getType() == 1) {
                    map.put("iosVersion", app.getVersion());
                }
                if (app.getType() == 2) {
                    map.put("androidVersion", app.getVersion());
                }
            }
            platform.put(privatePlatform.getCodevalue(), map);
        }
        memProvider.remove(ConstantBean.PLATFORM_PROVIDER_NAME);
        memProvider.put(ConstantBean.PLATFORM_PROVIDER_NAME, platform);
        setCachedPayValues();
        return platform;
    }

    /**
     * 设置吾享支付接口的缓存--适用所有马甲
     */
    public void setCachedPayValues() {
        memProvider.remove(ConstantBean.WUXIANG_CACHED_PAY_VALUES);
        memProvider.put(ConstantBean.WUXIANG_CACHED_PAY_VALUES, cachedPayValues);
        memProvider.remove(ConstantBean.WUXIANG_CACHED_TS_PAY_VALUES);
        memProvider.put(ConstantBean.WUXIANG_CACHED_TS_PAY_VALUES, cachedTsPayValues);
    }
}
